package com.taotao.rop.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeUtil {
    public static Long getDateMillis(Date date) {
        return date.getTime();
    }

    public static Long getDateStartMillis(Date date) {
        SimpleDateFormat shortDF = new SimpleDateFormat("yyyy-MM-dd");
        Long millis;
        try {
            millis = shortDF.parse(shortDF.format(date)).getTime();
        } catch (ParseException e) {
            millis = 0L;
            e.printStackTrace();
        }
        return millis;
    }

    public static Long getDateStartMillis(String date) {
        SimpleDateFormat shortDF = new SimpleDateFormat("yyyy-MM-dd");
        Long millis;
        try {
            millis = shortDF.parse(date).getTime();
        } catch (ParseException e) {
            millis = 0L;
            e.printStackTrace();
        }
        return millis;
    }

    public static Long getDateEndMillis(Date date) {
        SimpleDateFormat shortDF = new SimpleDateFormat("yyyy-MM-dd");
        Long millis;
        try {
            millis = shortDF.parse(shortDF.format(date)).getTime() + 24 * 3600 * 1000L -1;
        } catch (ParseException e) {
            millis = 0L;
            e.printStackTrace();
        }
        return millis;
    }

    /**
     * 日期date是否在start和end范围内
     * @param start 格式为"yyyy-MM-dd"
     * @param end 格式为"yyyy-MM-dd"
     * @param date 日期
     */
    public static boolean isActiveDate(String start, String end, Date date) {
        return !(compareDate(start, date) != 1 || compareDate(end, date) == 1);
    }

    /**
     * 比较两个时间的早晚
     * @param one 格式为"yyyy-MM-dd"
     * @param another 日期
     * @return 1: one > another; 0: one = another; -1: one < another
     */
    public static int compareDate(String one, Date another) {
        SimpleDateFormat shortDF = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date oneDate = shortDF.parse(one);
            if (oneDate.getTime() > another.getTime()) {
                return 1;
            }
            if (oneDate.getTime() == another.getTime()) {
                return 0;
            }

            if (oneDate.getTime() < another.getTime()) {
                return -1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -2;
    }

    public static String parseDate(Long millis) {
        SimpleDateFormat shortDF2 = new SimpleDateFormat("yyyy年MM月dd日");
        return shortDF2.format(new Date(millis));
    }
}
